/*
 * Copyright (C) hushenglan
 */

#ifndef _MYNET_EPOLL_POLL_H_
#define _MYNET_EPOLL_POLL_H_

namespace mynet
{

class CEpollPoll: public CPoll
{

public:

    /*
     * 构造函数
     */
    CEpollPoll(int maxOpenSockets, CServerSock *serverSock);

    /*
     * 析构函数
     */
    virtual ~CEpollPoll();

    /*
     * 创建事件模型
     */
    bool create();

    /*
     * 等待事件
     */
    int waitForEvents(int timeout);

    /*
     * 获取事件
     */
    CEvent* getEvent(int index);

    /*
     * 判断是否监听了此event
     */
    bool ifInReadEvent(CEvent* event);
    bool ifInWriteEvent(CEvent* event);
    bool ifInErrorEvent(CEvent* event);

    /*
     * 将此event加入监听
     */
    void insertReadEvent(CEvent* event);
    void insertWriteEvent(CEvent* event);
    void insertErrorEvent(CEvent* event);

    /*
     * 不再监听此event
     */
    void removeReadEvent(CEvent* event);
    void removeWriteEvent(CEvent* event);
    void removeErrorEvent(CEvent* event);

private:

    int _maxOpenSockets;
    int _epollFd;
    struct epoll_event *_events;
    int _availEventNum; /* 'protect mark' for once waitForEvents */
    CEpollEvent *_tmpEvent; /* attention: for return, not thread safe */
    CServerSock *_serverSock;
};

}

#endif
